package com.ygxsk.carhome.ui.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.listener.MyFeeLivingExpensesAddListener;
import com.ygxsk.carhome.response.FeeLivingExpensesBean;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.ClearEditTextTouch;

import java.math.BigDecimal;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-12-18  10:06
 * @Description:添加电费适配器，和水费要不要区分开？ FeeLivingExpensesBean 生活费用的总称呼
 */
public class FeeElectricFragmentAdapter extends BaseQuickAdapter<FeeLivingExpensesBean, BaseViewHolder> {

    //生活缴费的监听器
    private MyFeeLivingExpensesAddListener myFeeLivingExpensesAddListener;

    public void setMyFeeLivingExpensesAddListener(MyFeeLivingExpensesAddListener _myFeeLivingExpensesAddListener) {
        myFeeLivingExpensesAddListener = _myFeeLivingExpensesAddListener;
    }

    public FeeElectricFragmentAdapter(int layoutResId, @Nullable List<FeeLivingExpensesBean> data) {
        super(layoutResId, data);

        addChildClickViewIds(R.id.textViewAmontFifty, R.id.textViewAmontOneHundred, R.id.textViewAmontTwoHundred, R.id.textViewAmontFiveHundred);

    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, FeeLivingExpensesBean feeLivingExpensesBean) {

        if (feeLivingExpensesBean != null) {

            //条目是否选中
            boolean isChecked = feeLivingExpensesBean.isChecked();

            //名称 表名称
            String name = feeLivingExpensesBean.getGaugeName();

            //code 表编号
            String code = feeLivingExpensesBean.getGaugeNo();

            //费用
            BigDecimal deviceBalance = feeLivingExpensesBean.getDeviceBalance();

            //选中的button价格的 上次选中了哪个的记忆：
//            int indexPayButton = feeLivingExpensesBean.getPayFeeButton();

            //自定义的费用填写
            String customerEdit = feeLivingExpensesBean.getCustomerEdit();

            String balancePriceString = "";

            if (deviceBalance != null) {
//                balancePriceString = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + CommUtils.formatComma2BigDecimal(balancePriceString);
                balancePriceString = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + deviceBalance.toString();
            } else {
                balancePriceString = MMKV.defaultMMKV().decodeString(ConstantSign.P_CURRENCYSYMBOL, StringUtils.getString(R.string.yangjiaocurrencysymbol)) + CommUtils.formatComma2BigDecimal(CommUtils.valueOf(0.00)).toString();
            }

            //控件
            //选择框,
            CheckBox checkBoxInLiveAdapter = (CheckBox) baseViewHolder.getView(R.id.checkBoxInLiveAdapter);

            //表名称
            TextView textViewLiveNameEle = (TextView) baseViewHolder.getView(R.id.textviewlivenameele);

            //表号
            TextView textViewLiveCode = (TextView) baseViewHolder.getView(R.id.textviewlivecode);


            //余额度，应缴纳的额度
            TextView textViewBlance = (TextView) baseViewHolder.getView(R.id.textViewAmont);

            //50元
//            RoundTextView textViewAmontFifty = (RoundTextView) baseViewHolder.getView(R.id.textViewAmontFifty);
            //100元
//            RoundTextView textViewAmontOneHundred = (RoundTextView) baseViewHolder.getView(R.id.textViewAmontOneHundred);
            //200元
//            RoundTextView textViewAmontTwoHundred = (RoundTextView) baseViewHolder.getView(R.id.textViewAmontTwoHundred);
            //500元
//            RoundTextView textViewAmontFiveHundred = (RoundTextView) baseViewHolder.getView(R.id.textViewAmontFiveHundred);
            //自定义的输入文本
            ClearEditTextTouch editViewInputAmountCustomer = (ClearEditTextTouch) baseViewHolder.getView(R.id.editViewInputAmountCustomer);
//          EditText editViewInputAmountCustomer = (EditText) baseViewHolder.getView(R.id.editViewInputAmountCustomer);

            //电表名称
            CommUtils.setText(textViewLiveNameEle, name);
            //电表号码
            CommUtils.setText(textViewLiveCode, code);
            //剩余的金额
            CommUtils.setText(textViewBlance, balancePriceString);

            //对金额是正数或者是负数做对比
            //-----------------------------------------------------------------------------------------------------------------------------------------------
            BigDecimal numZero = BigDecimal.ZERO;

            if (deviceBalance != null) {

                if (deviceBalance.compareTo(numZero) < 0) {

                    //是负数，就是【欠费状态】，那么标记为红色字体，并且需要支付金额就是【所欠费的费用】
                    textViewBlance.setTextColor(ColorUtils.getColor(R.color.red_normal));

                    //如果价格小于0，那么当前需要缴费的额度默认就是该【余额】。
                    feeLivingExpensesBean.setAmount(deviceBalance.abs());

                } else {

                    textViewBlance.setTextColor(ColorUtils.getColor(R.color.dark));
//              feeLivingExpensesBean.setAmount(balancePrice);

                }
            }

            //-----------------------------------------------------------------------------------------------------------------------------------------------
            if (isChecked) {
                checkBoxInLiveAdapter.setChecked(true);
            } else {
                checkBoxInLiveAdapter.setChecked(false);
            }
            //-----------------------------------------------------------------------------------------------------------------------------------------------
            //签约人的监听器
            TextWatcher inputAmountCustomerTextWatcher = new TextWatcher() {

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable seditable) {

                    String input = seditable.toString();

                    if (!TextUtils.isEmpty(input)) {

                        if (myFeeLivingExpensesAddListener != null) {

                            if (feeLivingExpensesBean != null) {

                                //对输入的文本内容的监听
                                myFeeLivingExpensesAddListener.inputAmountNumber(input, getItemPosition(feeLivingExpensesBean), feeLivingExpensesBean);

                            } else {
                                ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                            }

                        }

                    } else {

                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "监听的--车辆车型的 文本为空");

                    }
                }
            };

            if ((editViewInputAmountCustomer.getTag() != null) && (editViewInputAmountCustomer.getTag() instanceof TextWatcher)) {
                //LogUtils.d(ConstantSign.LOGCAT_NOMAL + "removeTextChangedListener--removeTextChangedListener--removeTextChangedListener");
                editViewInputAmountCustomer.removeTextChangedListener((TextWatcher) editViewInputAmountCustomer.getTag());
            }

            //移除之后设置名称,动态设置名称,默认是空值
            editViewInputAmountCustomer.setText(customerEdit);
            //添加监听
            editViewInputAmountCustomer.addTextChangedListener(inputAmountCustomerTextWatcher);
            //设置标记,为了上面删除监听使用
            editViewInputAmountCustomer.setTag(inputAmountCustomerTextWatcher);

            //-----------------------------------------------------------------------------------------------------------------------------------------------

            //复选框的 点击状态
//            checkBoxInLiveAdapter.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View checkView) {
//
//                    LogUtils.d(ConstantSign.LOGCAT_NOMAL+"点击事件内容");

//                    if (checkBoxInLiveAdapter.isChecked()){

//                        checkBoxInLiveAdapter.setChecked(false);
//                    }else{

//                        checkBoxInLiveAdapter.setChecked(true);
//                    }

//                }
//            });


//            checkBoxInLiveAdapter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
//                    if (isChecked) {
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "被选 中了");
//                    } else {
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "被选 灭了 ");
//                    }
//
//                }
//            });

        }

    }
}
